package com.appserver.web.controller;

import com.alibaba.fastjson.JSON;
import com.appserver.web.dto.InitModel;
import com.liveinstars.common.exception.CommonException;
import com.liveinstars.common.utils.UserAgentUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 为了方便配置，需要登陆的接口统一路径
 */
@RestController
@RequestMapping("/wideopen")
public class WideOpenController {
    private static final Logger logger = LoggerFactory.getLogger(WideOpenController.class);

//    @Autowired
//    AppVersionService appVersionService;
//
//    @RequestMapping(path = "/v1/init", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String v1Init(@RequestBody @Validated InitModel model,
//                         @RequestHeader("from") String from,
//                         @RequestHeader("device_id") String deviceId,
//                         @RequestHeader("User-Agent") String userAgent) {
//        logger.info("/v1/init request {}, from {}, device_id {}", model.toString(), from, deviceId);
//        Map<String, Object> tokenInfo = new HashMap();
//        try {
//            checkInitParams(model);
//
//            String osVersionNum = userAgent != null ? UserAgentUtil.getOSVersionNum(userAgent) : "";
//
//            addInitInfo(model.getVersion(), from, tokenInfo);
//
//        } catch (Exception e) {
//            logger.error("初始化异常", e);
//            throw new CommonException("#init error", 500);
//        }
//        return JSON.toJSONString(tokenInfo);
//    }

    @RequestMapping(path = "/test", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String test() {
        logger.info("test api");
        Map<String, Object> tokenInfo = new HashMap();
        tokenInfo.put("test", "success");
        return JSON.toJSONString(tokenInfo);
    }

    @RequestMapping(path = "/activity", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String activity() {
        logger.info("test api");
        Map<String, Object> tokenInfo = new HashMap();
        tokenInfo.put("test", "success");
        return JSON.toJSONString(tokenInfo);
    }

//    @RequestMapping(path = "/test2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String test2() {
//        logger.info("test2 api");
//        User user = new User();
//        user.setId(1);
//        user.setRealName("ting");
//        user.setPhone("11111111");
//        Result result = new Result(true, "", user);
//        return result.toJsonString();
//    }
//
//
//    @RequestMapping(path = "/test3", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String test3() {
//        logger.info("test3 api");
//        User user = new User();
//        user.setId(1);
//        user.setRealName("ting");
//        user.setPhone("11111111");
//        ReturnT returnT = new ReturnT(user);
//        return returnT.toJsonString();
//    }
//    private void checkInitParams(InitModel model) {
//        if (model.isParamsAllEmpty()) {
//            throw new CommonException("#params error", 400);
//        }
//    }
//
//    private void addInitInfo(String version, String from, Map<String, Object> tokenInfo) {
//        Map<String, Object> versionInfo = appVersionService.checkAppVersion(version, from);
//        tokenInfo.putAll(versionInfo);
//    }




}
